package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.decoration.MammonRecord;
import com.yanqu.road.entity.player.UserBaseInfo;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class MammonRecordDaoImpl extends TempDataObjectDao<MammonRecord> {

	public Map<Integer, LinkedList<MammonRecord>> getMammonRecordMap(int maxCount) {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_u_mammon_record order by recordTime";
		Map<Integer, LinkedList<MammonRecord>> dataMap = new ConcurrentHashMap<>();
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				MammonRecord data = parse(rs);
				if(!dataMap.containsKey(data.getType())){
					dataMap.put(data.getType(), new LinkedList<>());
				}
				dataMap.get(data.getType()).addLast(data);
				data.setMammonIndex(dataMap.get(data.getType()).size());
			}
			for(Map.Entry<Integer, LinkedList<MammonRecord>> dataEntry : dataMap.entrySet()){
				while (dataEntry.getValue().size() > maxCount){
					dataEntry.getValue().removeFirst();
				}
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

	@Override
	protected boolean addData(MammonRecord data) {
		PreparedStatement call = null;
		String sql = "INSERT INTO t_u_mammon_record(recordUid,type,userId,userBaseInfo,recordTime,isSendCross) VALUES(?,?,?,?,?,?)";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setString(i++, data.getRecordUid());
			call.setInt(i++, data.getType());
			call.setLong(i++, data.getUserId());
			call.setString(i++, data.getUserBaseInfoJsonStr());
			call.setLong(i++, data.getRecordTime());
			call.setBoolean(i++, data.isSendCross());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	@Override
	protected boolean updateData(MammonRecord data) {
		PreparedStatement call = null;
		String sql = "UPDATE t_u_mammon_record SET type=?,userId=?,userBaseInfo=?,recordTime=?,isSendCross=? where recordUid=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setInt(i++, data.getType());
			call.setLong(i++, data.getUserId());
			call.setString(i++, data.getUserBaseInfoJsonStr());
			call.setLong(i++, data.getRecordTime());
			call.setBoolean(i++, data.isSendCross());
			call.setString(i++, data.getRecordUid());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	@Override
	protected MammonRecord parseData(ResultSet rs) throws SQLException {
		MammonRecord data = new MammonRecord();
		data.setRecordUid(rs.getString("recordUid"));
		data.setType(rs.getInt("type"));
		data.setUserId(rs.getLong("userId"));
		String baseUserInfoStr = rs.getString("userBaseInfo");
		if(null == baseUserInfoStr){
			baseUserInfoStr = "";
		}
		UserBaseInfo userBaseInfo = JSON.parseObject(baseUserInfoStr, new TypeReference<UserBaseInfo>() {});
		if(null != userBaseInfo) {
			data.setUserBaseInfo(userBaseInfo);
		}
		data.setRecordTime(rs.getLong("recordTime"));
		data.setSendCross(rs.getBoolean("isSendCross"));
		return data;
	}
}